package controller

import (
	"admin/model"
	"common/helper"
	"common/util"
	"encoding/json"
	"errors"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
)

type ConfigVipController struct {
	util.ControllerImpl
}

var ConfigVipModel = &model.ConfigVipModel{
	TableName: "tbl_cfg_vip",
}

func (that *ConfigVipController) GetModel() *model.ConfigVipModel {
	return ConfigVipModel
}
func (that *ConfigVipController) Insert(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := model.ConfigVipParams{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New("参数错误")
		}
		bl, err := that.GetModel().Exists(ent.Level)
		if err != nil {
			return err
		}
		if bl {
			return errors.New(helper.RecordExistErr)
		}
		err = that.GetModel().Insert(ent)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}
func (that *ConfigVipController) Update(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := model.ConfigVip{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New("参数错误")
		}
		err := that.GetModel().Update(ent)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}
func (that *ConfigVipController) Delete(ctx *fasthttp.RequestCtx) {
	that.ExecutionGetId(ctx, func(id uint32) error {
		err := that.GetModel().DeleteById(id)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}
func (that *ConfigVipController) Get(ctx *fasthttp.RequestCtx) {
	that.ExecutionGetId(ctx, func(id uint32) error {
		ent := model.ConfigVip{}
		err := that.GetModel().Get(id, &ent)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, ent)
		return nil
	})
}
func (that *ConfigVipController) GetByEx(ctx *fasthttp.RequestCtx, ex g.Expression) {
	ent := model.ConfigVip{}
	err := that.GetModel().GetByEx(ex, &ent)
	if err == nil {
		helper.RetData(ctx, true, ent)
		return
	}
	helper.RetData(ctx, false, err)
}
func (that *ConfigVipController) Page(ctx *fasthttp.RequestCtx) {
	that.ExecutionPage(ctx, func(page *model.ConfigVipPageParams) error {
		err := that.GetModel().Page(page)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, page)
		return nil
	})
}
func (that *ConfigVipController) ExecutionPage(ctx *fasthttp.RequestCtx, f func(page *model.ConfigVipPageParams) error) {
	that.Execution(ctx, func(params []byte) error {
		pageParams := &model.ConfigVipPageParams{}
		if e := json.Unmarshal(params, &pageParams); e != nil {
			return errors.New("参数错误")
		}
		return f(pageParams)
	})
}
